﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef Debug_h__
#define Debug_h__

#include "../utility/Singleton.h"
#include "../utility/MacroTool.h"

#include <windows.h>
#include <iostream>
#include <assert.h>

namespace SkyEngine2d
{
	//! 调试类
	class SGE_API Debuger final
		:public Singleton<Debuger>
	{
		Debuger();
		friend class Singleton<Debuger>;
	public:
		~Debuger();
		bool init();
		void setErrState();
		void setInforState();
		void setWarningState();
		void destory();
	private:
		HANDLE m_handle;
		FILE* m_in;
		FILE* m_out;
		FILE* m_err;
		bool m_isInit;
	};
	
}

#ifdef _DEBUG

#define _L(x) __L(x)
#define __L(x) L##x

#define DebugErr(fmt,...) do																				\
{																											\
	SkyEngine2d::Debuger::getInstance()->setErrState();																	\
	wprintf_s(L"[Err] funtion-> %ls ,file-> %ls At %d line\n", _L(__FUNCTION__),_L(__FILE__), __LINE__);	\
	wprintf_s(L##fmt,__VA_ARGS__ );																			\
	wprintf_s(L"\n");																						\
} while (0);

#define DebugInfor(fmt,...)do																				\
{																											\
	SkyEngine2d::Debuger::getInstance()->setInforState();																\
	wprintf_s(L"[Infor] ");																					\
	wprintf_s(L##fmt, __VA_ARGS__);																			\
	wprintf_s(L"\n");																						\
} while (0);

#define DebugWarning(fmt, ... )do 																			\
{																											\
	SkyEngine2d::Debuger::getInstance()->setWarningState();																\
	wprintf_s(L"[Warning] function-> %ls\n",_L(__FUNCTION__));												\
	wprintf_s(L##fmt, __VA_ARGS__);																			\
	wprintf_s(L"\n");																						\
} while (0);

#define DebugerAssert(v,err_info) do																		\
{																											\
	if (!(v))																								\
	{																										\
		DebugErr(err_info)																					\
	}																										\
	assert((v)&&(err_info));																				\
} while (0);

#else
#define DebugErr(fmt, ... ) 
#define DebugInfor(fmt , ...)
#define DebugWarning(fmt, ...) 
#define DebugerAssert(v,err_info)
#endif // _DEBUG 


#endif // Debug_h__
